class Axios{
	constructor(instanceConfig) {
		this.defaults = instanceConfig;
	}
	request(configOrUrl,config){
		if(typeof configOrUrl ==="string"){
			config = config ?? {};
			config.url = configOrUrl;
		}else{
			config = configOrUrl ?? {};
		}
		// 将config合并至this.defaults当中
		config = Object.assign({},this.defaults,config);
		const dispatchRequest = function(){
			return new Promise((resolve)=>{
				const xhr = new XMLHttpRequest();
				console.log(config)
				xhr.responseType = "json";
				xhr.open(config.type,config.url);
				xhr.send();
				xhr.onload = function(){
					resolve({
						data:xhr.response
					})
				}
			})
			
			
		}
		return dispatchRequest.call(this);
	}
}
function createInstance(defaultConfig){
	// 生成的Axios实例
	const context = new Axios(defaultConfig);
	// 将Axios.prototype.request函数中的this指向为Axios实例。
	const instance = Axios.prototype.request.bind(context);
	return instance;
}
const axios = createInstance({type:"get"});
export default axios;